﻿using PacoToolKit.Format;

namespace Protocol.PHORPProtocol.Encoder
{
    public class BaseEncoder
    {
        /// <summary>
        /// 创建报文
        /// </summary>
        /// <param name="DAddrReverse">设备地址，十进制</param>
        /// <param name="seqDec">seq序号、十进制</param>
        /// <param name="C">控制码 如：10</param>
        /// <param name="AddrDec">寄存器 偏移字节</param>
        /// <param name="DLenDec">数据长度</param>
        /// <param name="Data">数据内容Hex 主动下发的时候data填空</param>
        /// <returns></returns>
        public static string CreateMsg(string DAddrReverse, int seqDec, string C, int AddrDec, int DLenDec, string Data)
        {
            string res = "";
            string HEAD = "855A";
            string DTYPE = "00";
            string DAddr = Formater.ReverseHexString(Formater.DECtoHEX(DAddrReverse, 8));
            string seq = Formater.ReverseHexString(Formater.DECtoHEX(seqDec.ToString(), 4));

            string Addr = Formater.ReverseHexString(Formater.DECtoHEX(AddrDec.ToString(), 4));
            string DLen = Formater.ReverseHexString(Formater.DECtoHEX(DLenDec.ToString(), 4));

            int len = (C + Addr + DLen + Data).Length / 2 + 1;
            string Len = Formater.ReverseHexString(Formater.DECtoHEX(len.ToString(), 4)).ToUpper();
            string CS = BaseEncoder.CalcCSCode(Formater.HexStringToBytesKeep(HEAD + DTYPE + DAddr + seq + Len + C + Addr + DLen + Data)).ToString("X2");
            res = HEAD + DTYPE + DAddr + seq + Len + C + Addr + DLen + Data + CS;
            return res;
        }
        /// <summary>
        /// 创建报文
        /// </summary>
        /// <param name="DAddrReverse">设备地址，十进制</param>
        /// <param name="seqDec">seq序号、十进制</param>
        /// <param name="C">控制码 如：10</param>
        /// <param name="AddrDec">寄存器 偏移字节</param>
        /// <param name="DLenDec">数据长度</param>
        /// <param name="Data">数据内容Hex 主动下发的时候data填空</param>
        /// <returns></returns>
        public static string CreateAnswerMsg(string DAddrReverse, int seqDec, string C, int AddrDec, int DLenDec, string Data)
        {
            string res = "";
            string HEAD = "855A";
            string DTYPE = "00";
            string DAddr = Formater.ReverseHexString(Formater.DECtoHEX(DAddrReverse, 8));
            string seq = Formater.ReverseHexString(Formater.DECtoHEX(seqDec.ToString(), 4));
            string bin_C = Formater.DECtoBIN(Formater.HEXtoDEC(C), 8);
            bin_C = ("1" + bin_C.Substring(1, 7));
            C = Formater.DECtoHEX(Formater.BINtoDEC(bin_C));
            string Addr = Formater.ReverseHexString(Formater.DECtoHEX(AddrDec.ToString(), 4));
            string DLen = Formater.ReverseHexString(Formater.DECtoHEX(DLenDec.ToString(), 4));

            int len = (C + Addr + DLen + Data).Length / 2 + 1;
            string Len = Formater.ReverseHexString(Formater.DECtoHEX(len.ToString(), 4)).ToUpper();
            string CS = BaseEncoder.CalcCSCode(Formater.HexStringToBytesKeep(HEAD + DTYPE + DAddr + seq + Len + C + Addr + DLen + Data)).ToString("X2");
            res = HEAD + DTYPE + DAddr + seq + Len + C + Addr + DLen + Data + CS;
            return res;
        }

        public static byte CalcCSCode(byte[] bytes)
        {
            int temp = 0;
            for (int i = 0; i < bytes.Length; i++)
            {
                temp = temp + Formater.ToInt32(bytes[i]);
            }
            return Formater.ToByte(temp % 256);
        }
        public static string CalcCSCode(string str)
        {
            return Formater.CalcCSCode(str);
        }

    }
}
